/*
 * Copyright (C) 2011-2012 Andrew Wells
 *
 * Licensed under the GNU License, Version 3
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.gnu.org/copyleft/gpl.html
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 * Dungeons & Dragons Copyright (C) Wizards of the Coast. I do not claim
 * the right to any information used from their books or resources.
 */

package com.trace.wells.charactertracker;

import java.util.ArrayList;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class WeaponListCustomAdapter extends BaseAdapter {

	private ArrayList<Equipment> weaponList;
	private Context context;
	private LayoutInflater inflater;

	public WeaponListCustomAdapter(Context context, ArrayList<Equipment> equipment) {
		inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		this.context = context;

		weaponList = new ArrayList<Equipment>();

		for (Equipment e : equipment) {
			if (e instanceof Weapon) {
				Weapon tempWeapon = (Weapon) e;
				weaponList.add(tempWeapon);
			}
		}
	}

	public void addWeapon(Weapon w) {
		weaponList.add(w);
	}

	public void removeWeapon(Weapon w) {
		weaponList.remove(w);
	}

	@Override
	public int getCount() {
		return weaponList.size();
	}

	@Override
	public Equipment getItem(int arg0) {
		return weaponList.get(arg0);
	}

	@Override
	public long getItemId(int position) {
		return 0;
	}

	public class ViewHolder {
		TextView weaponName;
		TextView weaponDamage;
		TextView weaponCrit;
		TextView weaponAttackBonus;
		TextView weaponRange;
		TextView weaponWeight;
		TextView weaponCost;

	}

	public ArrayList<Equipment> getWeaponList() {
		return weaponList;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder;
		if (convertView == null) {
			holder = new ViewHolder();
			convertView = inflater.inflate(R.layout.weapon_list_view_item, null);

			holder.weaponName = (TextView) convertView.findViewById(R.id.weaponItemName);
			holder.weaponDamage = (TextView) convertView.findViewById(R.id.weaponItemDamage);
			holder.weaponCrit = (TextView) convertView.findViewById(R.id.weaponItemCritical);
			holder.weaponAttackBonus = (TextView) convertView.findViewById(R.id.weaponItemAttackBonus);
			holder.weaponRange = (TextView) convertView.findViewById(R.id.weaponItemRange);
			holder.weaponWeight = (TextView) convertView.findViewById(R.id.weaponItemWeight);
			holder.weaponCost = (TextView) convertView.findViewById(R.id.weaponItemCost);

			convertView.setTag(holder);
		} else
			holder = (ViewHolder) convertView.getTag();

		Weapon w = (Weapon) weaponList.get(position);

		holder.weaponName.setText(w.getName());
		holder.weaponDamage.setText(w.getDamageBonusString());
		holder.weaponCrit.setText(w.getCritical());
		holder.weaponAttackBonus.setText(w.getAttackBonusString());
		holder.weaponRange.setText(w.getRange());
		holder.weaponWeight.setText(w.getWeight());
		holder.weaponCost.setText(w.getValue());

		//convertView.refreshDrawableState();

		return convertView;
	}
}
